package cc.wsyw126.java.nowcoder.interview;

/**
 * Created by Administrator on 2017/4/24.
 * 给定两个字符串s1,s2,请返回bool值代表s2是否由s1旋转而成。
 * 字符串中字符为英文字母和空格，区分大小写，字符串长度小于等于1000。
 */
public class ReverseEqual {
    public boolean checkReverseEqual(String s1, String s2) {
        if (s1==null || s2 == null || s1.length() != s2.length()) return false;
        int position =0;
        for (int i = 0; i < s2.length(); i++) {
            if (s2.charAt(i) == s1.charAt(position)) {
                position++;
            } else if (position != 0){
                position =0;
                if (s2.charAt(i) == s1.charAt(position)) {
                    position++;
                }
            }
        }
        if (position ==0)return false;
        for (int i = 0; i < s2.length() && position <s1.length(); i++, position++) {
            if (s2.charAt(i) != s1.charAt(position)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        ReverseEqual reverseEqual = new ReverseEqual();
        boolean b = reverseEqual.checkReverseEqual("waterbottle", "erbottlewat");
        System.out.println("b = " + b);
    }
}
